package server.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import server.modules.generator.entity.MeetNoticeEntity;
import server.modules.generator.entity.MeetingEntity;
import server.modules.generator.entity.QiandaoEntity;
import server.modules.generator.service.MeetNoticeService;
import server.common.utils.PageUtils;
import server.common.utils.R;
import server.modules.generator.service.MeetingService;
import server.modules.generator.service.QiandaoService;
import server.modules.sys.controller.AbstractController;


/**
 * 通知记录
 *
 * @author 
 * @email 
 */
@RestController
@RequestMapping("generator/meetnotice")
public class MeetNoticeController  extends AbstractController {
    @Autowired
    private MeetNoticeService meetNoticeService;
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private QiandaoService qiandaoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:meetnotice:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = meetNoticeService.queryPage(params);

        return R.ok().put("page", page);
    }

 /**
     * 列表
     */
    @RequestMapping("/mylist")
    public R mylist(@RequestParam Map<String, Object> params){
        params.put("toUser",getUserId());
        PageUtils page = meetNoticeService.queryPage(params);

        return R.ok().put("page", page);
    }
    @RequestMapping("/myNoticeNum")
    public R myNoticeNum(@RequestParam Map<String, Object> params){
        params.put("toUser",getUserId());
        PageUtils page = meetNoticeService.queryPage(params);

        return R.ok().put("num", page.getTotalCount());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:meetnotice:info")
    public R info(@PathVariable("id") Integer id){
		MeetNoticeEntity meetNotice = meetNoticeService.getById(id);

        return R.ok().put("meetNotice", meetNotice);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:meetnotice:save")
    public R save(@RequestBody MeetNoticeEntity meetNotice){
		meetNoticeService.save(meetNotice);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:meetnotice:update")
    public R update(@RequestBody MeetNoticeEntity meetNotice){
		meetNoticeService.updateById(meetNotice);

        return R.ok();
    }

    /**
     * 签到
     */
    @RequestMapping("/qiandao")
    public R qiandao(@RequestBody MeetNoticeEntity meetNotice){


        MeetingEntity meetingEntity = meetingService.getById(meetNotice.getMeetId());
        if (null!=meetingEntity){
            if (!meetingEntity.getFlag().equals("进行中")){
                return R.error("会议"+meetingEntity.getFlag()+"不能签到！");
            }
        }


        meetNotice.setFlag("已签到");
		meetNoticeService.updateById(meetNotice);


        QiandaoEntity qiandaoEntity = new QiandaoEntity();
        qiandaoEntity.setCreateUser(getUserId());
        qiandaoEntity.setContext(meetNotice.getContext());
        qiandaoEntity.setCreatedTime(new Date());
        qiandaoEntity.setMeetid(meetNotice.getMeetId());
        qiandaoEntity.setPic(meetNotice.getPic());
        qiandaoService.save(qiandaoEntity);


        meetNoticeService.getById(meetNotice.getId());
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:meetnotice:delete")
    public R delete(@RequestBody Integer[] ids){
		meetNoticeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
